package org.hegang.design.second.builder.computer;

/**
 * 当类的可选参数达四个以上时，可使用构建者模式
 */
public class Computer {

    private String cpu;
    private String ram;
    private Integer usbCount;
    private String keyboard;
    private String display;


    private Computer(Builder builder) {
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.usbCount = builder.usbCount;
        this.keyboard = builder.keyboard;
        this.display = builder.display;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu='" + cpu + '\'' +
                ", ram='" + ram + '\'' +
                ", usbCount=" + usbCount +
                ", keyboard='" + keyboard + '\'' +
                ", display='" + display + '\'' +
                '}';
    }

    public static class Builder {
        private String cpu;//必须
        private String ram;//必须
        private Integer usbCount;//可选
        private String keyboard;//可选
        private String display;//可选

        public Builder(String cpu, String ram) {
            this.cpu = cpu;
            this.ram = ram;
        }

        public Builder setUsbCount(Integer usbCount) {
            this.usbCount = usbCount;
            return this;
        }

        public Builder setKeyboard(String keyboard) {
            this.keyboard = keyboard;
            return this;
        }

        public Builder setDisplay(String display) {
            this.display = display;
            return this;
        }

        public Computer builder() {
            return new Computer(this);
        }
    }
}
